package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindowEvent {

    private JFrame jf;

    public TestWindowEvent() {
        jf=new JFrame();
    }

    private void addEventHandler(){
        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int op=JOptionPane.showConfirmDialog(jf,"是否退出程序？","确认退出",JOptionPane.YES_NO_CANCEL_OPTION);
                if(JOptionPane.YES_OPTION==op){
                    System.exit(0);
                }
            }
        });
    }

    public void show(){
        addEventHandler();
        jf.setSize(400,300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestWindowEvent().show();
    }
}
